/**
 * 月报表记录
 * @constructor
 */
function MonthReportRecord() {
}

MonthReportRecord.init = function () {

    $('#tableList').bootstrapTable({
        url: '/monthReportRecord/tableSearch',
        method: 'post',
        striped : true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle : true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
        //search : "true",
        uniqueId: "id",
        pageList : [ 5, 10, 20,50,100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search:false,
        showColumns: true,
        showRefresh: true,
        showExport: true,
        //得到查询的参数
        queryParams: function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
                //********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
                //********
                //获取查询的对象，往下加入
                reportMonth: $('#search_ReportMonth').val(), // 请求时向服务端传递的参数
            };
            return query_params;
        },
        columns: [
            [
                {
                    title: '序号',//标题  可不加
                    align: "center",
                    width: 40,
                    align: 'center',
                    valign: 'middle',
                    width: '8%',
                    colspan:1,
                    rowspan:2,
                    formatter: function (value, row, index) {
                        var pageSize=$('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                        var pageNumber=$('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                    },
                },
                {
                    title: '企业名称',
                    field: 'enterpriseName',
                    align: 'center',
                    valign: 'middle',
                    colspan:1,
                    rowspan:2
                },
                {
                    title: '上报年月',
                    field: 'reportMonth',
                    align: 'center',
                    valign: 'middle',
                    colspan:1,
                    rowspan:2
                },
                {
                    title: '本月存栏数',
                    align: 'center',
                    valign: 'middle',
                    colspan:7,
                    rowspan:1
                },
                {
                    title: '本月产仔数',
                    align: 'center',
                    valign: 'middle',
                    colspan:2,
                    rowspan:1
                },
                {
                    title: '本月出栏数',
                    align: 'center',
                    valign: 'middle',
                    colspan:7,
                    rowspan:1
                },
                {
                    title: '本月无害化处理数',
                    align: 'center',
                    valign: 'middle',
                    colspan:7,
                    rowspan:1
                },
            ],
            [
                {
                    field: 'livestockSum',
                    title: "合计",
                    align: 'center',
                    valign: 'middle',
                    width: '6%',
                    formatter:function (value) {
                        return "<span style='color: red'>"+value+"</span>";
                    }
                },
                {
                field: 'maleLivestock',
                title: "种公畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'femaleLivestock',
                title: "后备母畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'breedFemaleLivestock',
                title: "能繁母畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'lactationLivestock',
                title: "哺乳牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'conservationLivestock',
                title: "保育牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'growLivestock',
                title: "育成牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            }, {
                    field: 'fetusNumber',
                    title: "产仔胎数",
                    align: 'center',
                    valign: 'middle',
                    width: '6%',
                },
                {
                    field: 'litterNumber',
                    title: "产仔数",
                    align: 'center',
                    valign: 'middle',
                    width: '6%',
                },{
                field: 'publicationSum',
                title: "合计",
                align: 'center',
                valign: 'middle',
                width: '6%',
                formatter:function (value) {
                    return "<span style='color: red'>"+value+"</span>";
                }
            },{
                field: 'malePublication',
                title: "种公畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'femalePublication',
                title: "后备母畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'breedFemalePublication',
                title: "能繁母畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'lactationPublication',
                title: "哺乳牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'conservationPublication',
                title: "保育牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'growPublication',
                title: "育成牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'harmlessSum',
                title: "合计",
                align: 'center',
                valign: 'middle',
                width: '6%',
                formatter:function (value) {
                    return "<span style='color: red'>"+value+"</span>";
                }
            },{
                field: 'maleHarmless',
                title: "种公畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            }
                ,{
                field: 'femaleHarmless',
                title: "后备母畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'breedFemaleHarmless',
                title: "能繁母畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'lactationHarmless',
                title: "哺乳牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'conservationHarmless',
                title: "保育牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            },{
                field: 'growHarmless',
                title: "育成牲畜",
                align: 'center',
                valign: 'middle',
                width: '6%',
            }
            ]
        ],
        onPreBody:function(data){

        }
    });
}
/**
 *时间选择器
 */
laydate.render({
    elem: '#text_ReportMonth',
    theme: 'molv',
    type:"month",
});
laydate.render({
    elem: '#search_ReportMonth',
    theme: 'molv',
    type:"month",
});
//查询方法****************************
function doSearch() {
    $("#tableList").bootstrapTable("refreshOptions",{pageNumber:1});
}
//重置****************************
function doRest() {
    $("#search_ReportMonth").val("");
    $("#tableList").bootstrapTable("refreshOptions",{pageNumber:1});
}
//增加方法****************************
var Action="";
var monthReportRecordId=0;
var pxt="/monthReportRecord"; //控制器名称
function addRecord() {
    harmlessId=0;
    Action="add";
    $("#myform")[0].reset();

}
// //增加/修改数据保存****************************
// function saveRecord() {
//     if(!validateForm()) return false;
//     //创建传递的参数
//     var postdata = {
//         monthReportRecordId: monthReportRecordId,
//         action:Action,
//         reportMonth: $("#text_ReportMonth").val()+'-01',  //上报年月
//         livestockReserve: $("#text_LivestockReserve").val(),  //本月后备母畜禽存栏数
//         livestockMenstruation: $("#text_LivestockMenstruation").val(),  //本月经产母畜禽存栏数
//         livestockShop: $("#text_LivestockShop").val(),  //本月商品母畜禽存栏数
//         fetusNumber: $("#text_FetusNumber").val(),    //本月产仔胎数
//         litterNumber: $("#text_LitterNumber").val(),    //本月产仔数
//         publicationMale: $("#text_PublicationMale").val(),    //本月种公畜禽出栏数
//         publicationFemale: $("#text_PublicationFemale").val(),    //本月种母畜禽出栏数
//         publicationShop: $("#text_PublicationShop").val(),    //本月商品畜禽出栏数
//         inspectionNumber: $("#text_InspectionNumber").val(),    //本月报检数
//         harmlessMale: $("#text_HarmlessMale").val(),    //本月种公畜禽无害化处理数
//         harmlessFemale: $("#text_HarmlessFemale").val(),    //本月种母畜禽无害化处理数
//         harmlessShop: $("#text_HarmlessShop").val(),    //本月商品无害化处理数
//     };
//     //发送异步请求到后台保存用户数据
//     $.ajax({
//         url:pxt+"/save",
//         type:"post",
//         data:JSON.stringify(postdata),
//         contentType:"application/json",
//         dataType:"json",
//         success:function(data){
//             if (data.code === "200") {
//                 $('#myModalAdd').modal('hide')
//                 doSearch();
//                 swal("操作成功!", "请继续操作", "success");
//             } else if(data.code === "501"){
//                 swal("操作失败!该月报表已填写!", "请重试", "error");
//             }else if(data.code=="502"){
//                 swal("操作失败!上月报表未填写!", "请先补填上月报表", "error");
//             }else{
//                 swal("操作失败!上", "请重试", "error");
//             }
//         }
//     });
// }

//操作相关方法****************************
function actionRecord(data,type) {
    if(type=="edit")
    {
        //修改
        monthReportRecordId=data.monthReportRecordId;
        Action="edit";
        $("#myform")[0].reset();
        $("#save").removeAttr("disabled");
        WriteData(data)
        $('#myModalAdd').modal('show')
    }
    else
    {
        //删除
        swal({
            title: "确认是否删除",
            text: "是否确认要删除该条记录？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
            //点击删除之后此处进行传递后台
            $.ajax({
                type: "delete",
                url: pxt+"/deleteById/"+data.monthReportRecordId,
                dataType : "json",
                contentType: "application/json",
                success: function (data, status) {
                    if (data.code == "200") {
                        doSearch();
                        swal("删除成功!", "请继续操作", "success");
                    }
                    else{
                        swal("删除失败!", "请重试", "error");
                    }
                }
            });

        });
    }
}

//给控件赋值****************************
function WriteData(data) {
    $("#text_ReportMonth").val(data.reportMonth);
    $("#text_LivestockReserve").val(data.livestockReserve);
    $("#text_LivestockMenstruation").val(data.livestockMenstruation);
    $("#text_LivestockShop").val(data.livestockShop);
    $("#text_FetusNumber").val(data.fetusNumber);
    $("#text_LitterNumber").val(data.litterNumber);
    $("#text_PublicationMale").val(data.publicationMale);
    $("#text_PublicationFemale").val(data.publicationFemale);
    $("#text_PublicationShop").val(data.publicationShop);
    $("#text_InspectionNumber").val(data.inspectionNumber);
    $("#text_HarmlessMale").val(data.harmlessMale);
    $("#text_HarmlessFemale").val(data.harmlessFemale);
    $("#text_HarmlessShop").val(data.harmlessShop);
}

//表单验证*****************************
function validateForm() {
    //validate方法参数可选
    return $("#myform").validate({
        rules: {
            text_ReportMonth: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            text_LivestockReserve: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_LivestockMenstruation: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_LivestockShop: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true, //必须是数字
            },
            text_FetusNumber: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_LitterNumber: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_PublicationMale: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_PublicationFemale: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_PublicationShop: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_InspectionNumber: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_HarmlessMale: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_HarmlessFemale: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
            text_HarmlessShop: {
                required: true,//必填
                minlength: 1, //最少1个字符
                number: true,  //必须是数字
            },
        },
        //错误信息提示
        messages: {
            text_ReportMonth: {
                required: "(必填)",
                minlength: "最少1位",
            },
            text_LivestockReserve: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_LivestockMenstruation: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_LivestockShop: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_FetusNumber: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_LitterNumber: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_PublicationMale: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_PublicationFemale: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_PublicationShop: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_InspectionNumber: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_HarmlessMale: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_HarmlessFemale: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
            text_HarmlessShop: {
                required: "(必填)",
                minlength: "最少1位",
                number: "必须输入数字",
            },
        },
    }).form();

}